home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / strings / searchlf.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-03  |  2.9 KB  |  103 lines

  1. ;unsigned short  search_left(strg,sub_strg,start_pt);
  2. ;  char  *strg,*sub_strg;
  3. ;  unsigned short  start_pt;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _search_left
  11. _search_left proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;save Turbo's DS
  17.     cmp  _memory_model,0    ;near or far?
  18.     jle  begin        ;jump if near
  19.     inc  bp            ;else add 2 to BP
  20.     inc  bp            ;
  21. begin:    cmp  _memory_model,2    ;data near or far?
  22.     jb   L0            ;jump if near    
  23.     les  di,dword ptr[bp+4] ;ES:DI pts to Strg
  24.     lds  si,dword ptr[bp+8] ;DS:SI pts to Substrg
  25.     add  bp,4        ;adjust stack pointer
  26.     jmp  short L00        ;jump ahead
  27. L0:    mov  ax,ds        ;near case
  28.     mov  es,ax        ;
  29.     mov  di,[bp+4]        ;
  30.     mov  si,[bp+6]        ;
  31. L00:    mov  bx,[bp+8]        ;startpoint in BX
  32.     inc  bx            ;count from 1
  33.     push di            ;calculate string length
  34.     mov  cx,0ffffh        ;counter
  35.     sub  al,al        ;look for ASCII 0
  36.     cld            ;direction forward
  37.     repne scasb        ;seek end
  38.     not  cx            ;reverse and adjust
  39.     dec  cx            ;length now in CX
  40.     pop di            ;restore Strg ptr
  41.     mov  bp,2        ;2 = Strg null
  42.     jcxz L6            ;quit if null
  43.     sub  ah,ah        ;    
  44.     push si            ;get length of substring
  45. L1:    cmp  byte ptr [si],0    ;
  46.     je   L2            ;
  47.     inc  ah            ;
  48.     inc  si            ;
  49.     jmp  short L1        ;
  50. L2:    pop  si            ;    
  51.     inc  bp            ;3 = Substrg null
  52.     or   ah,ah        ;test substring length
  53.     jz   L6            ;quit if null
  54.     add  di,bx        ;forward DI to startpoint
  55.     dec  di            ;adjust
  56.     mov  al,[si]        ;use char for SCAS below
  57.     inc  si            ;start SCAS from 2nd char
  58.     sub  cl,ah        ;strg len - substring len
  59.     sub  cx,bx        ; - startpoint
  60.     add  cx,3        ;adjust
  61.     dec  bp            ;2 = startpt out of range
  62.     cmp  cl,1        ;len-startpt big enough?
  63.     jle  L6            ;if not, quit routine
  64.     dec  bp            ;1 = not found
  65.     dec  bx            ;adjust BX for loop below
  66.     cld            ;direction flag forward
  67. L3:    mov  dx,cx        ;store num chars to scan
  68.     jcxz L5            ;in case Strg is 1 char
  69.     repne scasb        ;search the 1st char
  70.     jcxz L5            ;jump ahead if not found
  71.     sub  dx,cx        ;chars to scan - remainder
  72.     add  bx,dx        ;add to startpoint
  73.     push cx            ;save num chars to scan
  74.     mov  cl,ah        ;substring len in CX
  75.     push si            ;save string pointer
  76.     push di            ;save substring pointer
  77.     repe cmpsb        ;compare the substring
  78.     pop  di            ;restore string pointer
  79.     pop  si            ;restore substring pointer
  80.     jcxz L4            ;substring found if CX=0
  81.     pop  cx            ;set CX for next scan
  82.     jmp  short L3        ;not matching, try again
  83. L4:    pop  cx            ;since pushed before CMPS
  84.     mov  bp,0        ;0 = no error
  85.     jmp  short L7        ;found! quit routine
  86. L5:    mov  bp,1        ;error code when not found
  87. L6:    mov  bx,1        ;return 0 when not found (DECed)
  88. L7:    mov  ax,bx        ;place result for return
  89.     dec  ax            ;count from zero
  90.     pop  ds            ;restore DS
  91.     mov  bx,bp        ;get error return value
  92.     mov  _error_code,bl    ;set _error_code
  93.     pop  si            ;
  94.     pop  di            ;
  95.     pop  bp            ;
  96.     cmp  _memory_model,0    ;quit
  97.     jle  quit        ;
  98.     db   0CBh        ;RET far
  99. quit:    ret            ;RET near
  100. _search_left ENDP
  101. _TEXT    ENDS
  102.     END
  103.